package com.cos.shop.services.impl;

import com.cos.shop.dao.ProductDao;
import com.cos.shop.dao.impl.ProductDaoImpl;
import com.cos.shop.entity.PageHelper;
import com.cos.shop.entity.Product;
import com.cos.shop.services.ProductServices;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/2/24 9:08
 * @description:
 */
public class ProductServicesImpl implements ProductServices {
    private ProductDao pd = new ProductDaoImpl();

    @Override
    public Product getProductById(int pid) {
        try {
            if (pid>0) {
                return pd.getProductById(pid);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String, Object> getProductWithPageByTid(int pageIndex, int typeid) {
        try {
            //1 访问dao获取数据总条数
            Long count = pd.getProductCount(typeid);
            //2 创建pageHelper 当前页码 每页显示行数12 数据总条数
            PageHelper ph = new PageHelper(pageIndex,12,count.intValue());
            //3 请求分页数据
            List<Product> proList = pd.getProductPageByTid(typeid,ph.getStartRow(),ph.getPages());
            //4 创建map集合 存放 pageHelper proList
            Map<String, Object> maps = new HashMap<>();
            maps.put("glist",proList);
            maps.put("ph",ph);
            return maps;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> getProductByTid(int typeid) {
        try {
            if (typeid > 0) {
                return pd.getProductByTid(typeid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
